package com.example.helloandroidworld;

import java.util.Calendar;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.DatePickerDialog.OnDateSetListener;
import com.example.helloandroidworld.DatePickerFragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.widget.TimePicker;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
 
public class MessagingReminder extends SherlockFragmentActivity 
{	
	 Button timePicker, datePicker, RContacts, SaveReminder;
	 EditText Reminder, PhoneNumber;
	 int setHour, setMinute,interpretedHour;
	 int setYear, setMonth, setDay;
	 Calendar time;
	 AlarmManager am;
	 
	@Override
	public void onCreate( Bundle savedInstanceState )
	{ 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messagingreminder);
		am = (AlarmManager) this.getSystemService(ALARM_SERVICE );
		
		time = Calendar.getInstance();
		dialogSetUp(); // buttons are created in the dialogSetUP()
		inputAndButtonSetUp(); // set-up the rest of the items
		
		if(savedInstanceState != null )
		{
			fixTime(savedInstanceState);			
		}
		interpretDate();// alter button
		interpretHour();// alter button
	}
	
    public void onSaveInstanceState( Bundle savedInstanceState)
	{
    	super.onSaveInstanceState(savedInstanceState);
    	
    	// save the year and date info to set again after configuration changes
    	savedInstanceState.putInt("year", setYear);
		savedInstanceState.putInt("month", setMonth);
		savedInstanceState.putInt("day", setDay);
		
		savedInstanceState.putInt("hour", setHour);
		savedInstanceState.putInt("minute", setMinute);
	}
	
    // set-up btnRContacts, btnSaveReminder, txtReminder, reminderPhoneNo
    public void inputAndButtonSetUp()
    {
    	// bind buttons
    	RContacts = (Button) findViewById(R.id.btnRContacts);
    	SaveReminder = (Button) findViewById(R.id.btnSaveReminder);
    	// bind text views
    	Reminder = (EditText) findViewById(R.id.txtReminder);
    	PhoneNumber = (EditText) findViewById( R.id.reminderPhoneNo);
    	
    	// set-up Buttons
    	RContacts.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {// transitions screens
				startActivity( new Intent(MessagingReminder.this, MessagingContacts.class)); }});
    
    	SaveReminder.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String phoneNum = PhoneNumber.getText().toString();
				String messageText = Reminder.getText().toString();
				
				if( phoneNum.length() > 0 && messageText.length() > 0 )
					sendAlarm( phoneNum,messageText);
				else
					Toast.makeText(getBaseContext(), "Enter all requested input", Toast.LENGTH_SHORT).show();
			}});
    	
    }
    
    public void fixTime(Bundle savedInstanceState )
    {
    	
		setYear = savedInstanceState.getInt("year");
		setMonth = savedInstanceState.getInt("month");
		setDay = savedInstanceState.getInt("day");
		
		setHour = savedInstanceState.getInt("hour");
		setMinute = savedInstanceState.getInt("minute");
		
		time.set(Calendar.HOUR_OF_DAY, setHour);
		time.set(Calendar.MINUTE, setMinute);
		
		time.set(Calendar.YEAR, setYear);
		time.set(Calendar.MONTH, setMonth);
		time.set(Calendar.DAY_OF_MONTH, setDay);
    }
    
	/// stupid dialog code
	public void dialogSetUp()
	{
		// save current date and time!!
		
		setHour = time.get(Calendar.HOUR_OF_DAY);
		setMinute = time.get(Calendar.MINUTE);
		
		setYear = time.get(Calendar.YEAR);
		setMonth = time.get(Calendar.MONDAY);
		setDay = time.get(Calendar.DAY_OF_MONTH);
				
		// bind buttons
		timePicker = (Button) findViewById(R.id.pickTime);
		datePicker = (Button) findViewById(R.id.pickDate);

		timePicker.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showTimePicker();
			}});
		
		// display dialog to click
		datePicker.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDatePicker();
			}});
	}

	private void showDatePicker()
	{
		//instatiate fragment
		DatePickerFragment datePickerFrag = new DatePickerFragment();

		// place items in bundle
		Bundle args = new Bundle();
		args.putInt("year", setYear);
		args.putInt("month", setMonth);
		args.putInt("day", setDay);
		datePickerFrag.setArguments(args);
		
		datePickerFrag.setCallBack(ondate);
		datePickerFrag.show(getSupportFragmentManager(), "Date Picker");
	}
	
	private DatePickerDialog.OnDateSetListener ondate = new OnDateSetListener()
	{
		@Override
		public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth)
		{
			setYear = year;
			setMonth = monthOfYear;
			setDay = dayOfMonth;
			time.set(Calendar.YEAR, year);
			time.set(Calendar.MONTH, monthOfYear);
			time.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			interpretDate();
		}
	};
	
	private void showTimePicker()
	{
		//instatiate fragment
		TimePickerFragment timePickerFrag = new TimePickerFragment();

		// place items in bundle
		Bundle args = new Bundle();
		args.putInt("hour", setHour);
		args.putInt("minute", setMinute);
		timePickerFrag.setArguments(args);
		
		timePickerFrag.setCallBack(ontime);
		timePickerFrag.show(getSupportFragmentManager(), "Time Picker");
	}

	private TimePickerDialog.OnTimeSetListener ontime = new TimePickerDialog.OnTimeSetListener() 
	{
		public void onTimeSet(TimePicker view, int selectedHour,int selectedMinute) 
		{
			setHour = selectedHour;
			setMinute = selectedMinute;
			time.set(Calendar.HOUR_OF_DAY, selectedHour);
			time.set(Calendar.MINUTE, selectedMinute);
			interpretHour();
		}
	};
	
	// sets the user selected time for the buttons
	private void interpretHour()
	{	
		timePicker.setText(android.text.format.DateFormat.format("hh:mmaa", time));
	}
	
	private void interpretDate()
	{
				
		datePicker.setText(android.text.format.DateFormat.format("E, MM/dd/yy", time));
	}
	
	private void sendAlarm( String phoneNumber, String message)
	{
		Intent smSurprise = new Intent(this, smsAction.class);
		smSurprise.putExtra("phoneNumber", phoneNumber);
		smSurprise.putExtra("message", message);
		PendingIntent smsLater = PendingIntent.getBroadcast(this, 0, smSurprise, PendingIntent.FLAG_ONE_SHOT);
		am.set(AlarmManager.RTC_WAKEUP , time.getTimeInMillis(), smsLater);
		finish();
	}
	
}